<template>
  <div class="container">
    <div class="app-container">
      <!--    展示树形结构   -->
      <my-loading v-if="depts.length===0&&!timeout" />
      <el-tree v-else default-expand-all :data="depts" :props="defaultProps " :expand-on-click-node="false">
        <!--    节点结构    -->
        <template slot-scope="{ data }">
          <el-row style="width: 100%;height: 40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="6" style="display: flex;justify-content: space-between">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!--   $event 实参 在这里表示为操作类型（el-dropdown-item command="add"  中command的值）  -->
              <el-dropdown size="medium" @command="operateDept($event,data.id)">
                <span class="el-dropdown-link" style="align-self: end">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <revise-dialog ref="addDept" :dialog-type="dialogType" :title="dialogTitle" :all-department="allDepartment" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @update="getDepartment" />
  </div>
</template>

<script>
import { deleteDepartmentDetails, getDepartment } from '@/api/department'
import { transListToTree } from '@/utils'
import ReviseDialog from '@/views/department/components/revise-dialog.vue'
export default {
  name: 'Department',
  components: { ReviseDialog },
  props: {},
  data() {
    return {
      showDialog: false, // 显示弹层
      depts: [],
      allDepartment: [],
      currentNodeId: null, // 当前点击的id
      dialogTitle: '',
      dialogType: '',
      // 数据属性
      defaultProps: {
        label: 'name', // 显示的字段的名字
        children: 'children' // 读取子节点的字段名
      },
      timeout: false
    }
  },
  created() {
    this.getDepartment() // 调用获取数据的接口
  },
  methods: {
    async getDepartment() {
      this.timeout = false
      this.depts = []
      setTimeout(() => {
        this.timeout = true
      }, 3 * 1000)
      this.allDepartment = await getDepartment() // 导入的api
      this.depts = transListToTree(this.allDepartment, 0)
    },
    delDepartment(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteDepartmentDetails(id)
        this.getDepartment()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 操作部门方法
    operateDept(type, id) {
      this.currentNodeId = id
      this.dialogType = type
      if (type === 'add') {
        // 添加子部门
        this.showDialog = true
        this.dialogTitle = '添加部门信息'
      } else if (type === 'edit') {
        this.showDialog = true
        this.$nextTick(() => {
          this.$refs.addDept.gerDepartmentDetail()
          this.dialogTitle = '修改部门信息'
        })
        // this.$refs.addDept 等同于子组件的 this
      } else if (type === 'del') {
        this.delDepartment(id)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manager {
  width: 50px;
  display: inline-block;
  margin-right: 10px;
}

</style>
